package com.yixun.qfbao.config.ws;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

//@Component
public class ChannelWebSocketHandler extends TextWebSocketHandler {
    public static ConcurrentHashMap<String,WebSocketSession> webSocket = new ConcurrentHashMap<>();
    private static Logger logger= LoggerFactory.getLogger(ChannelWebSocketHandler.class);

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
        String uid = session.getAttributes().get("uid").toString();//存储用户id
        webSocket.put(uid,session);  //从set中删除
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, message);
        String uid = session.getAttributes().get("uid").toString();//存储用户id
        //接收消息
        if("ping".equals(message.getPayload())){
            webSocket.put(uid,session);
            //心跳信息
            TextMessage textMessage = new TextMessage("pong");
            session.sendMessage(textMessage);
        }
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        String receiveMsg = new String(message.asBytes());
        String uid = session.getAttributes().get("uid").toString();//存储用户id
        //检测短信
        if("ping".equals(receiveMsg)){
            webSocket.put(uid,session);
            TextMessage textMessage = new TextMessage("pong");
            session.sendMessage(textMessage);
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        String uid = session.getAttributes().get("uid").toString();//存储用户id
        webSocket.remove(uid,session);
    }

    //用于发送短信
    public static void sendMsg(String receiveId,String msg){
        if(!webSocket.containsKey(receiveId)){
            try{
                Thread.sleep(5000);
            }catch (Exception e){
            }

        }
        if(webSocket.containsKey(receiveId)) {
            WebSocketSession session = webSocket.get(receiveId);
            if(session.isOpen()){
                //发送消息
                TextMessage textMessage = new TextMessage(msg);
                try{
                    session.sendMessage(textMessage);
                }catch (IOException e){
                    logger.error("sendMsg error: {}", e);
                }
            }
        }
    }
}
